home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre1.z / postgre1 / test / postfs.usr.bin / cat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  1.1 KB  |  75 lines

  1. #include <stdio.h>
  2. #include <sys/file.h>
  3.  
  4. #include "tmp/libpq-fs.h"
  5.  
  6. int errs;
  7. void copy();
  8. extern char    *getenv();
  9. extern char    *PQexec();
  10.  
  11. void main(argc,argv)
  12.      int argc;       
  13.      char *argv[];
  14. {
  15.     int infd;
  16.     int cnt, total = 0;
  17.     int blen;
  18.     int i;
  19.     char *res;
  20.     char *dbname;
  21.  
  22.     if ((dbname = getenv("DATABASE")) == (char *) NULL) {
  23.     fprintf(stderr, "no database specified in env var DATABASE\n");
  24.     fflush(stderr);
  25.     exit (1);
  26.     }
  27.  
  28.     PQsetdb(dbname);
  29.  
  30.     if (argc == 1)    {
  31.     copy(0,1);
  32.     exit(0);
  33.     }
  34.  
  35.     res = PQexec("begin");
  36.     for (i=1;i<argc;i++) {
  37.          if (!strcmp(argv[i],"-")) {
  38.         copy(0,1);
  39.         continue;
  40.      }
  41.          infd = p_open(argv[i],O_RDONLY);
  42.      if (infd < 0) {
  43.         errs = 1;
  44.         continue;
  45.      }
  46.      if (pgcopy(infd,1) < 0) errs = 1;
  47.          p_close(infd);
  48.     }
  49.  
  50.     res = PQexec("end");
  51.     PQfinish();
  52. }
  53.  
  54. void copy(i,o)
  55. {
  56.     char buf[2048];
  57.     int n;
  58.     while ((n = read(i,buf,sizeof(buf))) > 0) {
  59.     write(o,buf,n);
  60.     }
  61. }
  62.  
  63. pgcopy(i,o)
  64. {
  65.     char buf[2048];
  66.     int n;
  67.     while ((n = p_read(i,buf,sizeof(buf))) > 0) {
  68.     write(o,buf,n);
  69.     }
  70.     if (n < 0)
  71.       return -1;
  72.     else 
  73.       return 0;
  74. }
  75.